package com.windaka.suizhi.service.impl;


import com.windaka.suizhi.annotation.NullToEmpty;
import com.windaka.suizhi.annotation.PageQuery;
import com.windaka.suizhi.constant.Constant;
import com.windaka.suizhi.constant.ExceptionConstant;
import com.windaka.suizhi.dao.AutoTestMapper;
import com.windaka.suizhi.exception.BaseException;
import com.windaka.suizhi.exception.ParamException;
import com.windaka.suizhi.exception.ResultException;
import com.windaka.suizhi.model.AutoTest;
import com.windaka.suizhi.model.dto.AutoTestDTO;
import com.windaka.suizhi.model.vo.AutoTestVO;
import com.windaka.suizhi.service.AutoTestService;
import com.windaka.suizhi.utils.CglibBeanCopierUtils;
import com.windaka.suizhi.utils.CheckParamUtils;
import com.windaka.suizhi.utils.ConvertUtils;
import com.windaka.suizhi.utils.RetPage;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
* AutoTestServiceImpl 增删改查
* @author wzk
* @date 2022-02-21 08:56:53
* @param
* @return
* @other
**/
@Service
@RequiredArgsConstructor
public class AutoTestServiceImpl implements AutoTestService {

    private final AutoTestMapper autoTestMapper;

    /**
    * 查询
    * @author wzk
    * @date 2022-02-21 08:56:53
    * @param autoTestDTO 传入的参数
    * @return RetPage<?> 返回的结果
    * @other
    **/
    @PageQuery
    @Override
    public RetPage<AutoTestVO> getAutoTest(AutoTestDTO autoTestDTO) throws BaseException {
        AutoTest autoTest = new AutoTest();
        CglibBeanCopierUtils.copy(autoTestDTO, autoTest);
        List<AutoTest> dataList = autoTestMapper.selectAutoTest(autoTest);
        List<AutoTestVO> resultList = new ArrayList<>();
        for (AutoTest each : dataList) {
            AutoTestVO eachVO = new AutoTestVO();
            // copy same field
            CglibBeanCopierUtils.copy(each, eachVO);
            resultList.add(eachVO);
        }
        return RetPage.result(resultList);
    }

    /**
    * 新增
    * @author wzk
    * @date 2022-02-21 08:56:53
    * @param autoTestDTO 传入的参数
    * @return String 返回的结果
    * @other
    **/
    @Override
    public String addAutoTest(AutoTestDTO autoTestDTO) throws BaseException {
        AutoTest autoTest = new AutoTest();
        CglibBeanCopierUtils.copy(autoTestDTO, autoTest);
        int result = autoTestMapper.insertAutoTest(autoTest);
        if (result <= 0) {
            // SQL执行未生效
            throw new ResultException(ExceptionConstant.DEFAULT_MAPPER_EXCEPTION_MSG);
        }
        return Constant.DEFAULT_ADD_SUCCESS;
    }

    /**
    * 更新
    * @author wzk
    * @date 2022-02-21 08:56:53
    * @param autoTestDTO 传入的参数
    * @return String 返回的结果
    * @other
    **/
    @Override
    public String updAutoTest(AutoTestDTO autoTestDTO) throws BaseException {
        AutoTest autoTest = new AutoTest();
        // copy dto to model
        CglibBeanCopierUtils.copy(autoTestDTO, autoTest);
        if (CheckParamUtils.isNullOrEmpty(autoTest.getId())) {
            throw new ParamException("缺少id");
        }
        int result = autoTestMapper.updateAutoTest(autoTest);
        if (result <= 0) {
            // SQL执行未生效
            throw new ResultException(ExceptionConstant.DEFAULT_MAPPER_EXCEPTION_MSG);
        }
        return Constant.DEFAULT_UPDATE_SUCCESS;
    }

    /**
    * 删除
    * @author wzk
    * @date 2022-02-21 08:56:53
    * @param autoTestDTO 传入的参数
    * @return String 返回的结果
    * @other
    **/
    @Override
    public String delAutoTest(AutoTestDTO autoTestDTO) throws BaseException {
        AutoTest autoTest = new AutoTest();
        // copy dto to model
        CglibBeanCopierUtils.copy(autoTestDTO, autoTest);
        if (CheckParamUtils.isNullOrEmpty(autoTest.getId())) {
            throw new ParamException("缺少id");
        }
        int result = autoTestMapper.deleteAutoTest(autoTest);
        if (result <= 0) {
            // SQL执行未生效
            throw new ResultException(ExceptionConstant.DEFAULT_MAPPER_EXCEPTION_MSG);
        }
        return Constant.DEFAULT_DELETE_SUCCESS;
    }
}
